package com.bjpowerndoe.ylb.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.bjpowerndoe.ylb.common.AppConsts;
import com.bjpowerndoe.ylb.map.UserAccountMap;
import com.bjpowerndoe.ylb.web.enums.CodeEnum;
import com.bjpowerndoe.ylb.web.model.CommonResult;
import com.bjpowerndoe.ylb.web.model.dto.UserDTO;
import com.bjpowerndoe.ylb.web.model.param.RealnameParam;
import com.bjpowerndoe.ylb.web.model.param.UserParam;
import com.bjpowerndoe.ylb.web.model.vo.UserAccountVO;
import com.bjpowerndoe.ylb.web.service.inter.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.data.convert.DtoInstantiatingConverter;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 *
 */
@Api(tags = "用户功能")
@RequiredArgsConstructor
@RestController
public class UserController {

    private final UserService userService;

    //注册 vue提交的是json格式数据， 使用java对象接收json
    @PostMapping("/user/register")
    public CommonResult userRegister(@RequestBody UserParam userParam){
        CommonResult commonResult = CommonResult.fail(CodeEnum.REQUEST_PARAM_ERR);
        if( userParam.checkData() ){
            //验证短信验证码
            boolean check = userService.checkSmsCode(AppConsts.SMS_ACTION_REGISTER,
                    userParam.getPhone(),userParam.getCode());
            if( check ){
                //进行注册
                CodeEnum registerResult = userService.userRegister(userParam.getPhone(), userParam.getSecret());
                commonResult.setCodeEnum(registerResult);
            } else {
                commonResult = CommonResult.fail(CodeEnum.SMS_CODE_INVALID);
            }
        }
        return commonResult;

    }

    //登录，获取token ，使用post请求，json数据，同注册一样
    @PostMapping("/user/login")
    public  CommonResult userLogin(@RequestBody UserParam userParam){
        CommonResult commonResult = CommonResult.fail(CodeEnum.REQUEST_PARAM_ERR);
        if( userParam.checkData()){
            //验证登录验证码
            boolean check = userService.checkSmsCode(AppConsts.SMS_ACTION_LOGIN,
                    userParam.getPhone(),userParam.getCode());
            if( check ){
                //执行登录
                UserDTO userDTO = userService.userLogin(userParam.getPhone(),userParam.getSecret());
                if(userDTO  != null ){ //登录成功
                    //生成token
                    String token = UUID.randomUUID().toString().replaceAll("-", "");
                    //存储token到redis
                    boolean save = userService.saveTokenToRedis(userDTO,token);
                    if( save ){ //服务器准备好token，将token和一些数据返回给vue
                        Map<String,Object> data = new HashMap<>();
                        data.put("token",token);
                        data.put("uid", userDTO.getId());
                        data.put("phone",userDTO.getPhone());
                        data.put("name",userDTO.getName());
                        commonResult = CommonResult.ok(data);
                    }
                } else {
                    commonResult.setCodeEnum(CodeEnum.PHONE_PASSWORD_ERR);
                }
            } else {
                commonResult = CommonResult.fail(CodeEnum.SMS_CODE_INVALID);
            }
        }
        return commonResult;
    }


    //实名认证
    @ApiOperation("实名认证")
    @PostMapping("/user/realname")
    public CommonResult realname(@RequestBody RealnameParam realnameParam,
                                 @RequestHeader("uid") Integer uid){
        CommonResult commonResult = CommonResult.fail(CodeEnum.REQUEST_PARAM_ERR);
        if( realnameParam.checkData() ){
            //判断是否已经做过实名认证
            UserDTO userDTO  = userService.queryUserById(uid);
            if(StrUtil.isNotBlank(userDTO.getName())){
                //不能再次认证
                commonResult.setCodeEnum(CodeEnum.REALNAME_DOUBLE_ERR);
            } else {
                //去认证
                boolean realname = userService.userRealname(uid,
                        realnameParam.getName(),realnameParam.getIdCard());
                if( realname ){
                    commonResult.setCodeEnum(CodeEnum.OK);
                } else {
                    commonResult.setCodeEnum(CodeEnum.FAIL);
                }
            }
        }

        return commonResult;
    }
    //查询用户和资金
    @ApiOperation("查询用户和资金")
    @GetMapping("/user/info")
    public CommonResult queryUserAccountById(@RequestHeader("uid") Integer uid){
        CommonResult commonResult = CommonResult.fail(CodeEnum.REQUEST_PARAM_ERR);
        if( uid > 0 ){
            UserAccountMap userAccountMap = userService.queryUserAccountByUid(uid);
            commonResult = CommonResult.ok(BeanUtil.copyProperties(userAccountMap, UserAccountVO.class));
        }
        return commonResult;
    }
}
